package com.yy.jhl.ui.manager.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yy.common.base.BaseAdapter;
import com.yy.common.commonutils.ImageLoaderUtils;
import com.yy.common.commonutils.StringUtil;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.InvitePackageInfo;
import com.yy.jhl.bean.InviteUploadInfo;
import com.yy.jhl.bean.InviteUploadInfo;
import com.yy.jhl.ui.main.activity.PhotosShowActivity;
import com.yy.jhl.ui.manager.activity.InviteUploadActivity;
import com.yy.jhl.ui.manager.activity.OrderDetailActivity;
import com.yy.jhl.ui.manager.activity.OrderFlowActivity;
import com.yy.jhl.video.MoviePlayActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Ellis.Li on 2017/6/15.
 */

public class UploadAdapter extends BaseAdapter {
    private List<InviteUploadInfo> modelList;
    boolean isEdit = false;
    public UploadAdapter(Context context,boolean isEdit) {
        super(context);
        this.isEdit = isEdit;
        modelList = new ArrayList<>();
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
        notifyDataSetChanged();
    }

    @Override
    public void removeItem(int position) {
        super.removeItem(position);
        modelList.remove(position);
        notifyDataSetChanged();
    }
    @Override
    public void clearData() {
        super.clearData();
        modelList.clear();
        notifyDataSetChanged();
    }
    public void addData(List<InviteUploadInfo> monomerModels) {
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }

    public void addAllData(List<InviteUploadInfo> monomerModels) {
        clearData();
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return modelList.size();
    }

    @Override
    public Object getItem(int i) {
        return modelList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Holder storeHolder;
        if(view == null){
            storeHolder = new Holder();
            view = layoutInflater.inflate(R.layout.item_upload,null);
            storeHolder.initView(view);
            view.setTag(storeHolder);
        }else{
            storeHolder = (Holder)view.getTag();
        }
        final InviteUploadInfo itemModel = modelList.get(i);
        if (isEdit){
            storeHolder.manager_text.setVisibility(View.VISIBLE);
        }else {
            storeHolder.manager_text.setVisibility(View.GONE);
        }
        storeHolder.package_code_text.setText("箱号："+itemModel.getPack_num());
        storeHolder.package_note_text.setText(itemModel.getRemark());
        storeHolder.package_address_text.setText(itemModel.getAddress());
        storeHolder.package_time_text.setText(itemModel.getUpdate_time());
        final ArrayList<String> attachPaths = new ArrayList<>();
        final String attach = itemModel.getImage();
        if (StringUtil.isEmpty(itemModel.getImage())){
            storeHolder.noScrollgridview.setVisibility(View.GONE);
        }else {
            storeHolder.noScrollgridview.setVisibility(View.VISIBLE);
            PictureAdapter pictureAdapter = new PictureAdapter(mContext,false);
            storeHolder.noScrollgridview.setAdapter(pictureAdapter);
            storeHolder.noScrollgridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                        long arg3) {
                    if (arg2 == attachPaths.size()) {
                    } else if (attachPaths.get(arg2).endsWith("mp4")){
                        //小视频
                        MoviePlayActivity.startAction(mContext,attachPaths.get(arg2));
                    }else {
                        //预览
                        PhotosShowActivity.startAction(mContext,attachPaths,arg2);
                    }
                }
            });
            if (!StringUtil.isEmpty(attach)){
                String[] arrays = attach.split("\\#\\*\\#");
                for (int m =0;m<arrays.length;m++){
                    attachPaths.add(arrays[m]);
                }
                pictureAdapter.setSelectPaths(attachPaths);
                StringUtil.setGridViewHeightBasedOnChildren(storeHolder.noScrollgridview,attachPaths.size(),4);
            }
//            ImageLoaderUtils.display(mContext,storeHolder.package_image,itemModel.getImage());
//            storeHolder.package_image.setOnClickListener(new OnNoDoubleClickListener() {
//                @Override
//                protected void onNoDoubleClick(View v) {
//                    ArrayList<String> paths = new ArrayList<String>();
//                    paths.add(itemModel.getImage());
//                    PhotosShowActivity.startAction(mContext,paths,0);
//                }
//            });
        }
        storeHolder.manager_text.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                InvitePackageInfo packageInfo = new InvitePackageInfo();
                packageInfo.setPack_num(itemModel.getPack_num());
                packageInfo.setPackage_id(itemModel.getPackage_id());
                packageInfo.setBatch_id("");
                InviteUploadActivity.startAction(mContext,packageInfo);
            }
        });

        return view;
    }
    /**
     *
     */
    class Holder{
        private TextView package_code_text;
        private ImageView package_image;
        private TextView package_note_text;
        private TextView package_address_text;
        private TextView manager_text;
        private TextView package_time_text;
        private LinearLayout pic_parent;
        private GridView noScrollgridview;
        void initView(View view) {
            package_code_text = (TextView) view.findViewById(R.id.package_code_text);
            package_image = (ImageView) view.findViewById(R.id.package_image);
            package_note_text = (TextView) view.findViewById(R.id.package_note_text);
            package_address_text = (TextView) view.findViewById(R.id.package_address_text);
            manager_text = (TextView) view.findViewById(R.id.manager_text);
            package_time_text = (TextView) view.findViewById(R.id.package_time_text);
            pic_parent = (LinearLayout) view.findViewById(R.id.pic_parent);
            noScrollgridview = (GridView) view.findViewById(R.id.noScrollgridview);
        }

    }
}
